<template>
  <!-- 欠料单配件处理 -->
  <div>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartInventoryPartInventoryComponentsInDebtPart"
      height="auto"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :isColumnFilter="false"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>
    <div class="btnWrap">
      <el-button @click="handleClose">关闭</el-button>
    </div>
  </div>
</template>

<script>
import { queryInventoryPartIsRelevanceInDebt, inDebtPartClickDone } from '@/api/part/partInventory/partInventory';
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import { getAllUser, getStorageList } from '@/api/part/common';

export default {
  mixins: [dictionary],
  props: ['stockInNo'],
  data() {
    return {
      columns: this.createTableColumns(),
      fetchapi: queryInventoryPartIsRelevanceInDebt,
      fetchParams: { xhrAbort: true },
      salerData: {}, // 售前人员信息 翻译用
      storageData: {} // 仓库信息 翻译用
    };
  },
  created() {
    // console.log(this.stockInNo);
    // if (this.stockInNo) {
    this.fetchParams = Object.assign(this.fetchParams, {
      xhrAbort: false,
      stockInNo: this.stockInNo
    });
    this.getAllUserData();
    this.getAllStorageData();
    // }
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'caozuo',
          render: params => {
            return (
              <el-button
                type="text"
                disabled={params.row.closeStatus == '10041001'}
                onClick={() => {
                  this.handleConfirm(params.row);
                }}
              >
                完成
              </el-button>
            );
          }
        },
        {
          title: '单据号码',
          dataIndex: 'sheetNo'
        },
        {
          title: '缺料类型',
          dataIndex: 'shortType',
          render: props => {
            return <span>{this.createDictText(props.row.shortType, '8134')}</span>;
          }
        },
        {
          title: '欠料数量',
          dataIndex: 'shortQuantity'
        },
        {
          title: '配件代码',
          dataIndex: 'partNo'
        },
        {
          title: '配件名称',
          dataIndex: 'partName'
        },
        {
          title: '仓库',
          dataIndex: 'storageCode',
          render: params => {
            return <span>{this.storageData[params.row.storageCode]}</span>;
          }
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName'
        },
        {
          title: '缺料时间',
          dataIndex: 'createdAt',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '经手人',
          dataIndex: 'handler',
          render: params => {
            return <span>{this.salerData[params.row.handler]}</span>;
          }
        }
      ];
    },
    // 点击完成
    async handleConfirm(row) {
      let res = await inDebtPartClickDone({
        shortId: row.shortId
      });
      if (res.resultCode == 200) {
        notifyAction('修改成功', 'success');
        this.reSearchTableData();
      }
    },
    // 关闭
    handleClose() {
      this.$emit('close');
    },
    // 重新查询table数据
    reSearchTableData() {
      this.fetchParams.xhrAbort = true;
      this.fetchParams.xhrAbort = false;
    },
    // 查询所有员工
    async getAllUserData() {
      let res = await getAllUser();
      let obj = {};
      res.data.map(item => {
        obj[item.userId] = item.employeeName;
      });
      this.salerData = obj;
    },
    // 获取所有仓库
    async getAllStorageData() {
      let res = await getStorageList();
      let obj = {};
      res.data.map(item => {
        obj[item.STORAGE_CODE] = item.STORAGE_NAME;
      });
      this.storageData = obj;
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
